package com.lideru.blog.controlle;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lideru.blog.entity.Blog;
import com.lideru.blog.entity.Comment;
import com.lideru.blog.entity.User;
import com.lideru.blog.service.IBlogService;
import com.lideru.blog.service.ICommentService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.HashMap;

@Controller
public class BlogContrller {
    @Autowired
    private IBlogService blogService;
    @Autowired
    private ICommentService commentService;

    @GetMapping("ViewBlog")
    public String ViewBlog(Model model, Integer id) {
        Blog blog = blogService.getById(id);
        model.addAttribute("blog", blog);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>();
        queryWrapper.eq("blog_id", id);
        model.addAttribute("commentList", commentService.list(queryWrapper));
        UpdateWrapper<Blog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.setSql("view = view + 1 ");
        updateWrapper.eq("id", id);
        blogService.update(updateWrapper);
        return "blog";
    }

    @GetMapping("hot")
    public String hot(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) model.addAttribute("user", user);
        else model.addAttribute("user", null);
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>();
        queryWrapper.select().orderByDesc("view");
        model.addAttribute("blog", blogService.list(queryWrapper));
        return "hot";
    }

    @GetMapping("new")
    public String newById(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) model.addAttribute("user", user);
        else model.addAttribute("user", null);
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>();
        queryWrapper.select().orderByDesc("id");
        model.addAttribute("blog", blogService.list(queryWrapper));
        return "new";
    }

    @PostMapping("search")
    public String search(String title, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) model.addAttribute("user", user);
        else model.addAttribute("user", null);
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>();
        queryWrapper.select().like("title", title);
        queryWrapper.select().orderByDesc("id");
        model.addAttribute("blog", blogService.list(queryWrapper));
        return "search";
    }

    @RequestMapping("writeBlog")
    public String writeBlog(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user != null) model.addAttribute("user", user);
        else return "/login";
        return "writeBlog";
    }

    @RequestMapping("myBlog")
    public String myBlog(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println("用户：" + user);
        if (user != null) model.addAttribute("user", user);
        else return "/login";

        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>();
        queryWrapper.eq("user_id", user.getId());
        model.addAttribute("blog", blogService.list(queryWrapper));
        return "myBlog";
    }

    @PostMapping("addBlog")
    @ResponseBody
    public String addBlog(@RequestBody Blog blog, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return JSONArray.toJSONString("nologin");
        }
        System.out.println(user);
        System.out.println(blog);
        blog.setUserId(user.getId());
        blog.setAuthor(user.getUsername());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        blog.setTime(sdf.format(System.currentTimeMillis()));
        if (blogService.save(blog)) return JSONArray.toJSONString("succeed");
        return JSONArray.toJSONString("error");
    }

    @GetMapping("deleteBlog")
    public String deleteBlog(Long Id, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null)
            return "/login";
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id", user.getId());
        map.put("id", Id);
        if (blogService.removeByMap(map))
            return "redirect:/myBlog";
        else
            return "error";

    }

    @GetMapping("editBlogPage")
    public String editBlog(Long Id, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null)
            return "/login";
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<Blog>();
        queryWrapper.eq("user_id", user.getId());
        queryWrapper.eq("id", Id);
        queryWrapper.last("limit 1");
        Blog blog = blogService.list(queryWrapper).get(0);
        if (blog == null) return "error";
        model.addAttribute("blog", blog);
        return "editBlog";
    }

    @PostMapping("editBlog")
    @ResponseBody
    public String editBlog(@RequestBody Blog blog, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return JSONArray.toJSONString("nologin");
        }
        if (blogService.updateById(blog)) return JSONArray.toJSONString("succeed");
        return JSONArray.toJSONString("error");
    }


}
